import { useReducer } from 'react';
/**
 * 定义初始状态信息
 * @loading 加载标记
 */
export interface IstateType {
  loading: boolean;
  leftVersionType: string;
}

/**
 * 新增和编辑页面操作枚举
 * @UPDATE_ADD 新增
 * @UPDATE_EDIT 编辑
 * @UPDATE_CANCEL 作废
 * @CLEAR_RECEIVE 取消modal框,清空存入的接收部门数据
 *
 */
export enum IActionEnum {
  UPDATE_ADD = 'add',
  UPDATE_EDIT = 'edit',
  UPDATE_CANCEL = 'cancel',
  CLEAR_RECEIVE = 'clearAllReceive',
  LEFT_VERSIONTYPE = 'leftVersionType', //左侧版本类型
}

/**
 * 定义新增和编辑页面操作匹配
 * @type 操作类型
 * @payload 传递数据
 */
export interface IActionType {
  type: IActionEnum;
  payload?: any;
}

/** 定义版本新增和编辑状态管理 */
export const useUpdateReducer = () => {
  /** 初始化状态 */
  const initState: IstateType = {
    loading: false,
    leftVersionType: '',
  };

  /** 状态管理逻辑处理 */
  const updateReducer = (state: IstateType, action: IActionType) => {
    switch (action?.type) {
      case IActionEnum.UPDATE_ADD:
        return {
          ...state,
          loding: action?.payload,
        };
      case IActionEnum.UPDATE_EDIT:
        return {
          ...state,
          loading: action?.payload,
        };
      case IActionEnum.UPDATE_CANCEL:
        return {
          ...state,
          loading: action?.payload,
        };
      /**左侧版本类型 */
      case IActionEnum.LEFT_VERSIONTYPE:
        return {
          ...state,
          leftVersionType: action?.payload,
        };
      default:
        return {
          ...state,
          ...initState,
        };
    }
  };

  const [state, dispatch] = useReducer(updateReducer, initState);

  return {
    ...state,
    dispatch,
  };
};
